package cn.yuanqiao.es.config;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.es.config
 * @ClassName: EsClientPoolFactory
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/6/30 14:19
 * @Version: 1.0
 */
@Component
public class EsClientPoolFactory implements PooledObjectFactory<ElasticsearchClient> {
    private static final Logger log = LoggerFactory.getLogger(EsClientPoolFactory.class);
    @Autowired
    EsClientConfig esClientConfig ;

    @Override
    public PooledObject<ElasticsearchClient> makeObject() {
      //  ElasticSearchConfig esClientConfig= new ElasticSearchConfig();
        String[] hosts =EsClientConfig.getUrisStatic().split(",");
        List<HttpHost> httpHostList = new ArrayList<>();
        for (String host : hosts) {
            httpHostList.add(HttpHost.create(host));
        }
        HttpHost[] httpHosts = httpHostList.toArray(new HttpHost[0]);
        RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);

        final CredentialsProvider credentialsProvider =
                new BasicCredentialsProvider();
        //设置账号和密码
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(EsClientConfig.getUsernameStatic(),
                EsClientConfig.getPasswordStatic()));
        restClientBuilder.setHttpClientConfigCallback(httpClientBuilder ->
                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        restClientBuilder.setRequestConfigCallback(req -> {
            req.setConnectionRequestTimeout(EsClientConfig.ES_CONNECTION_REQ_TIMEOUT);
            // 与服务器建立连接的超时时间
            req.setConnectTimeout(EsClientConfig.ES_CONNECTION_TIMEOUT);
            // 网络通讯的超时时间
            req.setSocketTimeout(EsClientConfig.ES_SOCKET_TIMEOUT);
            return req;
        });
        RestClient restClient = restClientBuilder.build();
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
        return new DefaultPooledObject<>(elasticsearchClient);
    }


    @Override
    public void activateObject(PooledObject<ElasticsearchClient> pooledObject) throws Exception {

    }

    @Override
    public void destroyObject(PooledObject<ElasticsearchClient> pooledObject) throws Exception {
        ElasticsearchClient elasticsearchClient = pooledObject.getObject();

    }


    @Override
    public void passivateObject(PooledObject<ElasticsearchClient> pooledObject) throws Exception {

    }

    @Override
    public boolean validateObject(PooledObject<ElasticsearchClient> pooledObject) {
        return false;
    }
}
